uniform sampler2D terrainTex;

uniform int shadowToggle;



varying float height;



varying vec4 lightFactor;

varying vec4 TexCoords0;



uniform sampler2D ShadowMap;

varying vec4 ShadowCoord;



void main()

{

if (height > 4.0)

   gl_FragColor+=vec4(0.0,0.0,1.0,0.0);

else

   gl_FragColor=texture2D(terrainTex, TexCoords0.st);

    

     if (shadowToggle == 1) {

         vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w ;

         

        // Used to lower moiré pattern and self-shadowing

        shadowCoordinateWdivide.z += 0.0005;

        

        

        float distanceFromLight = texture2D(ShadowMap,shadowCoordinateWdivide.st).z;

        

        

        float shadow = 1.0;

        if (ShadowCoord.w > 0.0)

            shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.5 : 1.0 ;

        

        gl_FragColor *=  shadow;
}
}